﻿using System;
using System.Net;
using System.ServiceModel.Channels;
using System.ServiceModel;
using SilverlightRestServices.Description;

namespace SilverlightRestServices.Channels
{
    public class RestMessageEncodingBindingElement : MessageEncodingBindingElement
    {
        public override MessageVersion MessageVersion
        {
            get;
            set;
        }

        public RestMessageEncodingBindingElement()
        {
            this.MessageVersion = MessageVersion.None;
        }

        private RestMessageEncodingBindingElement(MessageVersion messageVersion)
        {
            this.MessageVersion = messageVersion;
        }

        protected RestMessageEncodingBindingElement(RestMessageEncodingBindingElement original) : base(original)
        {
            this.MessageVersion = original.MessageVersion;
        }

        public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
        {
            context.BindingParameters.Add(this);
            return context.BuildInnerChannelFactory<TChannel>();
        }

        public override MessageEncoderFactory CreateMessageEncoderFactory()
        {
            return new RestMessageEncoderFactory(this.MessageVersion);
        }

        public override BindingElement Clone()
        {
            return new RestMessageEncodingBindingElement(this);
        }
    }
}
